import Vue from "vue";
import Router from "vue-router";

Vue.use(Router);

/* Layout */
import Layout from "@/layout";

export const constantRoutes = [
  {
    path: "/redirect",
    component: Layout,
    hidden: true,
    children: [
      {
        path: "/redirect/:path(.*)",
        component: () => import("@/views/redirect/index")
      }
    ]
  },
  // 首页
  {
    path: "/",
    component: () => import("@/views/school/index"),
    hidden: true
  },
  {
    path: "/login",
    component: () => import("@/views/login/index"),
    hidden: true
  },
  {
    path: "/auth-redirect",
    component: () => import("@/views/login/auth-redirect"),
    hidden: true
  },
  {
    path: "/404",
    component: () => import("@/views/error-page/404"),
    hidden: true
  },
  {
    path: "/401",
    component: () => import("@/views/error-page/401"),
    hidden: true
  },
  {
    path: "/dashboard",
    component: Layout,
    redirect: "/dashboard/index",
    children: [
      {
        path: "/dashboard/index",
        component: () => import("@/views/dashboard/index"),
        name: "Dashboard",
        meta: { title: "概览", icon: "dashboard", affix: true }
      }
    ]
  }
];

export const asyncRoutes = [
  // 课程
  {
    path: "/course",
    component: Layout,
    redirect: "/course/media",
    alwaysShow: true,
    name: "Course",
    meta: {
      title: "课程",
      icon: "excel",
      roles: ["admin", "editor"] // you can set roles in root nav
    },
    children: [
      {
        path: "media",
        component: () => import("@/views/course/media"),
        name: "MediaCourse",
        meta: {
          title: "图文"
        }
      },
      {
        path: "audio",
        component: () => import("@/views/course/audio"),
        name: "AudioCourse",
        meta: {
          title: "音频"
        }
      },
      {
        path: "video",
        component: () => import("@/views/course/video"),
        name: "VideoCourse",
        meta: {
          title: "视频"
        }
      },
      {
        path: "column",
        component: () => import("@/views/course/column"),
        name: "ColumnCourse",
        meta: {
          title: "专栏"
        }
      }
    ]
  },

  // 用户
  {
    path: "/user",
    component: Layout,
    children: [
      {
        path: "user",
        component: () => import("@/views/user/user"),
        name: "User",
        meta: { title: "用户", icon: "user" }
      }
    ]
  },

  // 交易
  {
    path: "/pay",
    component: Layout,
    redirect: "/pay/order",
    alwaysShow: true,
    name: "Pay",
    meta: {
      title: "交易",
      icon: "education",
      roles: ["admin", "editor"] // you can set roles in root nav
    },
    children: [
      {
        path: "order",
        component: () => import("@/views/pay/order"),
        name: "OrderPay",
        meta: {
          title: "订单管理"
        }
      },
      {
        path: "assets",
        component: () => import("@/views/pay/assets"),
        name: "AssetsPay",
        meta: {
          title: "资产管理"
        }
      },
      {
        path: "payment",
        component: () => import("@/views/pay/payment"),
        name: "PaymentPay",
        meta: {
          title: "支付管理"
        }
      }
    ]
  },

  // 可视化
  {
    path: "/renovation",
    component: Layout,
    redirect: "/renovation/mobile_index",
    alwaysShow: true,
    name: "Renovation",
    meta: {
      title: "可视化",
      icon: "education",
      roles: ["admin", "editor"] // you can set roles in root nav
    },
    children: [
      {
        path: "mobile_index",
        component: () => import("@/views/renovation/mobile_index"),
        name: "Mobile_indexRenovation",
        meta: {
          title: "移动端"
        }
      },
      {
        path: "pc_index",
        component: () => import("@/views/renovation/pc_index"),
        name: "Pc_indexRenovation",
        meta: {
          title: "PC端"
        }
      }
    ]
  },

  // 营销
  {
    path: "/marketing",
    component: Layout,
    redirect: "/marketing/group",
    alwaysShow: true,
    name: "Marketing",
    meta: {
      title: "营销",
      icon: "education",
      roles: ["admin", "editor"] // you can set roles in root nav
    },
    children: [
      {
        path: "group",
        component: () => import("@/views/marketing/group"),
        name: "GroupMarketing",
        meta: {
          title: "拼团"
        }
      },
      {
        path: "flashsale",
        component: () => import("@/views/marketing/flashsale"),
        name: "FlashsaleMarketing",
        meta: {
          title: "秒杀"
        }
      },
      {
        path: "coupon",
        component: () => import("@/views/marketing/coupon"),
        name: "CouponMarketing",
        meta: {
          title: "优惠券"
        }
      }
    ]
  },

  // 工具
  {
    path: "/tool",
    component: Layout,
    redirect: "/tool/group",
    alwaysShow: true,
    name: "Tool",
    meta: {
      title: "工具",
      icon: "education",
      roles: ["admin", "editor"] // you can set roles in root nav
    },
    children: [
      {
        path: "bbs",
        component: () => import("@/views/tool/bbs"),
        name: "BbsTool",
        meta: {
          title: "小社群"
        }
      },
      {
        path: "question",
        component: () => import("@/views/tool/question"),
        name: "QuestionTool",
        meta: {
          title: "题库"
        }
      },
      {
        path: "testpaper",
        component: () => import("@/views/tool/testpaper"),
        name: "TestpaperTool",
        meta: {
          title: "试卷"
        }
      },
      {
        path: "book",
        component: () => import("@/views/tool/book"),
        name: "BookTool",
        meta: {
          title: "电子书"
        }
      }
    ]
  },

  // 设置
  {
    path: "/setting",
    component: Layout,
    redirect: "/setting/setting_index",
    alwaysShow: true,
    name: "Setting",
    meta: {
      title: "设置",
      icon: "education",
      roles: ["admin", "editor"] // you can set roles in root nav
    },
    children: [
      {
        path: "setting_index",
        component: () => import("@/views/setting/setting_index"),
        name: "Setting_indexSetting",
        meta: {
          title: "店铺设置"
        }
      },
      {
        path: "staffs",
        component: () => import("@/views/setting/staffs"),
        name: "StaffsSetting",
        meta: {
          title: "员工管理"
        }
      },
      {
        path: "role",
        component: () => import("@/views/setting/role"),
        name: "RoleSetting",
        meta: {
          title: "角色管理"
        }
      }
    ]
  },

  // icon
  /* {
    path: "/icon",
    component: Layout,
    children: [
      {
        path: "index",
        component: () => import("@/views/icons/index"),
        name: "Icons",
        meta: { title: "Icons", icon: "icon", noCache: true }
      }
    ]
  }, */

  // 404 page must be placed at the end !!!
  { path: "*", redirect: "/404", hidden: true }
];

const createRouter = () =>
  new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes
  });

const router = createRouter();

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher; // reset router
}

export default router;
